<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
class ClassadminController extends AdminbaseController {
	
	protected $m;
	protected $yct;
	protected $districtService;
	protected $user;
	protected $usersLogic;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclClass');
		$this->yct = D('YgclClassType');
		$this->districtService = D('District','Service');
		$this->classService = D('Class','Service');
		$this->user = D('Users');
		$this->usersLogic = D('Users','Logic');
	}
	
    public function index(){
    	$this->districtoptions = $this->districtService->_get_select_options($_GET['did']);
    	$this->display();
    }
    
    public function edit(){
    	$id = I('id');
    	if($id){
    		$map['id'] = $id;
    		$this->info = $this->m->where($map)->find();
    		$uid = D('UsersDistrictRelation')->where(array('cid'=>$this->info['id']))->getField('uid',true);
    		$this->uid = $uid;
    		$this->did = $this->info['did'];
    	}else{
    		$this->did = I('did');
    	}
    	$this->teachers = $this->user->where(array('role'=>'teacher'))->field('uid,realname')->select();
    	//$this->districtoptions = $this->districtService->_get_select_options($this->info['did']);
    	$this->display();
    }
    
    public function edit_post(){
    	if(IS_POST){
    		$did = $_POST['did'];
    		//添加课程
    		$data_class = array(
    			'title' => $_POST['title'],
    			'desc' => $_POST['desc'],
    			'listorder' => $_POST['listorder'],
    			'content' => $_POST['content'],
    			'did' => $did
    		);
    		$cid = $_POST['id'];
    		if($cid){
    			$data_class['id'] = $cid;
    			$data_class['update_time'] = Date('Y-m-d H:i:s');
    			if($this->m->create($data_class)){
    				$this->m->save();
    			}
    		}else{
    			$data_class['create_time'] = Date('Y-m-d H:i:s');
    			$data_class['update_time'] = Date('Y-m-d H:i:s');
    			if($this->m->create($data_class)){
    				$cid = $this->m->add();
    			}
    		}
    		//更新班级缓存
    		$this->classService->_create_cache_class();
    		
    		//保存教师与课程之间的关系
    		$uid = $_POST['uid'];
    		if($uid){
    			$this->usersLogic->_save_relation($uid,$did,$cid);
    		}
    		$this->success('保存成功');
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		$this->classService->_create_cache_class();
    		$this->success('删除成功',U('index'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changestatus(){
    	$data[$this->m->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($this->m->save($data)){
    		$this->classService->_create_cache_class();
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
    
    public function get_class() {
    	$did = I('post.did');
    	$map['did'] = $did;
    	$map['status'] = 1;
    	$data = D('YgclClass')->where($map)->field('id,title')->select();
    	if($data){
    		$this->success($data);
    	}else{
    		$this->error();
    	}
    }	
    
    
    public function classtypelist(){	
    	$this->cid = I('get.cid'); 
    	$this->classname = D('YgclClass')->where('id = '.$this->cid)->getField('title');
    	$this->display();
    }
    
    public function editclasstype(){
    	
    	$id = I('get.id');
    	if($id){
    		$map['id'] = $id;
    		$this->info = $this->yct->where($map)->find();
    	}
    	$this->cid = I('get.cid');
    	$this->display();
    }
    
    public function editclasstype_post(){
    	if(IS_POST){
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		if($_POST['id']){
    			if($this->yct->create()){
    				if($this->yct->save()){
    					$this->classService->_create_cache_classtype($_POST['cid']);
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$_POST['create_time'] = $_POST['update_time'];
    			if($this->yct->create()){
    				$id = $this->yct->add();
    				if($id){
    					$this->classService->_create_cache_classtype($_POST['cid']);
    					$this->success('添加成功');
    				}else{
    					$this->error('添加失败');
    				}
    			}
    		}
    	}
    }
    
    public function changeclasstypestatus(){
    	$data[$this->yct->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	$cid = $this->yct->where(array('id'=>$_REQUEST['id']))->getField('cid');
    	if($this->yct->save($data)){
    		$this->classService->_create_cache_classtype($cid);
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
    
    public function delclasstype(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$cid = $this->yct->where(array('id'=>$_REQUEST['id']))->getField('cid');
    	if($this->yct->where($map)->delete()){
    		$this->classService->_create_cache_classtype($cid);
    		$this->success('删除成功',U('classtypelist',array(cid=>$cid)));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function childlist(){
    	$cid = I('cid');
    	$this->title = $this->m->where(array('id'=>$cid))->getField('title');
    	$this->display();
    }
    
    public function getteacherinfo($cid){
    	if($cid){
    		$uid = D('UsersDistrictRelation')->where(array('cid'=>$cid))->getField('uid',true);
    		if($uid){
    			$datas =  D('Users')->where(array('uid'=>array('in',$uid)))->select();
    			foreach ($datas as $dt){
    				$html .= $dt['realname'].' '.$dt['mobile'].'<br/>';
    			}
    		}
    	}
    	return $html;
    }
    
    public function checkusername(){
    	$username = trim($_REQUEST['username']);
    	$mobile = trim($_REQUEST['mobile']);
    	//dump($username);
    	//dump($mobile);
    	//exit();
    	$this->user->where(array('username'=>$username))->find();
    	if($this->regService->checkInvitation($value)){
    		$data['ok'] = '';
    	}else{
    		$data['error'] = '邀请码错误';
    	}
    	$this->ajaxReturn($data,'JSON');
    }
}